home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
optivc32
/
fitdemo.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-03-06
|
25KB
|
481 lines
/*********************** FITDEMO.CPP **********************************
* *
* Data-fitting demo program for *
* O p t i V e c *
* with Borland C++ 4.x, 5.x, C++ Builder, *
* or Microsoft Visual C++ 5.0 or 6.0 *
* *
* Copyright 1996-1999 by Martin Sander *
* *
* *
* This sample program is meant to provide you with some basic *
* examples of code for the use of OptiVec's data-fitting routines. *
* Especially for the non-linear and multi-experiment functions, *
* the user will have to adapt this code to his specific problem. *
* *
**************************************************************************/
/*
Borland C++, Command-line:
a) 32-bit: type
BCC32 -W fitdemo.cpp vcf3w.lib
b) 16-bit: type
BCC -ml -W fitdemo.cpp vcl3w.lib mcl3w.lib
Borland C++, IDE:
1. Open the new-project menu with Project/New.
2. Create a project FITDEMO in the OptiVec directory,
e.g., \bc\optivec.
3.a) 32-bit: Choose Application[EXE] for Win32 GUI, static linking,
single-thread.
3.b) 16-bit: Choose Application[EXE] for Windows3.x, memory model LARGE.
4. Hit OK.
5. In the Project window that will now be on the screen, delete the
nodes FITDEMO.DEF and FITDEMO.RC.
6.a) 32-bit: Add the node VCF3W.LIB.
6.b) 16-bit: Add the nodes VCL3W.LIB and MCL3W.LIB.
7. In Options/Project, be sure the include-file search path and the
library search path both include the respective OPTIVEC directories,
e.g.: \bc\optivec\include and bc\optivec\lib, resprectively
8. Compile and run.
Microsoft Visual C++:
1. Create a new project as a "Win32 application".
2. In the project settings, C/C++, Code Generation,
verify that single-thread debug is chosen.
3. Add \OptiVec\include to the include-file search path.
4. Add the files FITDEMO.CPP and OVVCSD.LIB to your project.
5. Compile and run.
*/
#include <windows.h> /* Compiler's include files */
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <VDstd.h> /* OptiVec include files */
#include <VDmath.h>
#include <MDstd.h>
#include <VIstd.h>
#include <Vgraph.h>
HWND hWndMain;
int vview;
dVector XExp, X2, YExp, YFit, YExp2, YFit2, YExp3, YFit3;
ui sizex;
double FitPars[7]; // the highest number of parameters we will have in our examples
int ParStatus[7];
#define polydeg 5 // refers to the polynomial fitting example
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
NEWMATHERR // this macro has an effect only for 16-bit BC
LONG FAR PASCAL MainMessageHandler (HWND, UINT, WPARAM, LPARAM);
// the following function is used with VD_linfit:
static void PolyModel( dVector BasFuncs, double x, unsigned nfuncs )
{ /* This function fills the vector BasFuncs with powers of x.
VD_linfit will then determine the coefficient for each of these
powers.
Note that the coefficients do not occur in the model function!
The basis functions with known coefficients (whose fitting has
been disabled) are still part of the model and must be calculated.
You will see below that the values of the known (disabled)
coefficients must be set prior to calling VD_linfit. */
BasFuncs[0] = 1.0;
for( unsigned i=1; i<nfuncs; i++ )
BasFuncs[i] = BasFuncs[i-1]*x;
}
// the following function is used with VD_nonlinfit:
static void VPolyModel( dVector Y, dVector X, ui size )
{ /* Here, the model function has to fill a whole result vector,
using your first guess of FitPars. In contrast to the
linear case, now the coefficients are explicitly used in the
model function. You must initialize FitPars with something,
even if you have no idea about the result.
FitPars must be global, so that the model function can access the
parameters. With VD_nonlinfit, you can use just any functions
in your model.
For better comparison, we use the same polynomial approximation
as before, but now we code it as if we didn't know that a
polynomial actually is linear in its coefficients (and as if
we didn't know either how to efficiently code a polynomial at all). */
double xi;
for( ui i=0; i<size; i++ )
{
xi = X[i];
Y[i]= FitPars[0]
+ FitPars[1] * xi
+ FitPars[2] * xi * xi
+ FitPars[3] * xi * xi * xi
+ FitPars[4] * xi * xi * xi * xi
+ FitPars[5] * xi * xi * xi * xi * xi;
}
}
// the following function is used with VD_multiNonlinfit:
static void VSineModel( dVector Y, dVector X, ui size, unsigned nExperiment )
{ /* According to the parameter nExperiment, the model function must
choose the correct parameters for the calculation of the model
function. The model function itself is the same for all experiments. */
double omega, phase, amp;
switch( nExperiment )
{
case 0: phase = FitPars[1];
amp = FitPars[4];
break;
case 1: phase = FitPars[2];
amp = FitPars[5];
break;
case 2: phase = FitPars[3];
amp = FitPars[6];
}
omega = FitPars[0]; // we assume this parameter to be the same
VDx_sin( Y, X, size, omega, phase, amp );
}
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR /* lpCmdLine */, int /* nCmdShow */)
{
MSG msg; /* MSG structure to pass to windows proc */
WNDCLASS wc;
char *AppName;
AppName = "FitDemo";
if(!hPrevInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc= MainMessageHandler;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, AppName);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = AppName;
wc.lpszClassName = AppName;
RegisterClass (&wc);
}
/* create application's Main window: */
hWndMain = CreateWindow (AppName,
"OptiVec Data-Fitting Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* Use default X, Y, and width */
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, /* Parent window's handle */
NULL, /* Default to Class Menu */
hInstance, /* Instance of window */
NULL); /* Create struct for WM_CREATE */
if (hWndMain == NULL)
{
MessageBox(NULL, "Could not create window in WinMain", NULL, MB_ICONEXCLAMATION);
return (1);
}
ShowWindow(hWndMain, SW_SHOWMAXIMIZED); /* Display main window */
UpdateWindow(hWndMain);
while(GetMessage(&msg, NULL, 0, 0)) /* Main message loop */
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass (AppName, hInstance);
return (msg.wParam);
}
LONG FAR PASCAL MainMe